#include "../StdInc.h"
#include <boost/format.hpp>
using namespace boost;

namespace GSTrader
{
	namespace TradeBase
	{
		CTransaction::CTransaction() : m_bCompleted(false), m_dQty(0), m_dAvePrice(0), m_dCommission(0), m_nOrderId(0), m_nInstrumentId(0), m_nSide(0),
			m_dAmount(0), m_dPrice(0)
		{
		}

		CTransaction::~CTransaction()
		{
		}

		CTransaction::CTransaction( CExecutionReport& report) : CTransaction()
		{
			m_nOrderId		= report.m_nOrderId;
			m_nInstrumentId = report.m_nInstrumentId;
			m_nSide			= report.m_nSide;

			Add(report);
		}

		void CTransaction::Add(CExecutionReport& report)
		{
			m_listTradeId.push_back(report.m_nTradeId);

			m_dQty += report.m_dLastQty;
			m_dAmount += report.m_dLastPrice * report.m_dLastQty;
			m_dPrice = report.m_dLastPrice;

			if (m_dQty > 0)
			{
				m_dAvePrice = m_dAmount / m_dQty;
			}
			m_dCommission += report.m_dCommission;
		}

		string CTransaction::ToString()
		{
			format fmt("OrderId:%d,AvePrice:%.2f,Qty:%.0f,Amount:%.0f,Number:%d");
			fmt % m_nOrderId;
			fmt % m_dAvePrice;
			fmt % m_dQty;
			fmt % m_dAmount;
			fmt % (int)m_listTradeId.size();

			return fmt.str();

		}
	}
}